% GAUTIER LE BIHAN - 2020
% Replication files for "Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector 
% Menu-Cost Model ?" Review of Economics and Statistics
%
% Estimation of Aggregate 3-sector CP model 
% inputs for IRF Table 4
% inputs for estimation of product level parameters where the aggregate
% sector is a 3-sector model

clear all
close all
clear matrix
clc

addpath('..\..\Utilities')  
load actual_moments_k

load res_estim_prod3l
p=res_estim_prod3l
param0=   [p(1)   p(4) p(7)  ;
          p(2) 	 p(5)	 p(8);
           p(3) p(6) p(9)	
]
		
actual_moments=[    0.1001	    0.6362	    0.0496	    0.0848	    4.5187;
    0.0589	    0.8652	    0.0475	    0.0555	    7.2764;
    0.6812	    0.6110	    0.0314	    0.0581	    2.8919];
		

actual_std=[    0.0010	 ;   0.0053	  ;  0.0007	  ;  0.0019	  ;  0.1419;
                0.0009	 ;   0.0054	  ;  0.0009	 ;   0.0016	  ;  0.4870;
                0.0009	 ;   0.0054	  ;  0.0009	 ;   0.0016	  ;  0.4870];
                %0.0012	 ;   0.0021	 ;   0.0001	 ;   0.0003	  ;  0.0254];

 actual_var= actual_std.* actual_std*1000;

 for jj=1;

   p01 =param0(1,1);
   p02 =param0(2,1);
     p03 =param0(3,1);
 
    mu_c01 =param0(1,2);
    mu_c02 =param0(2,2);
    mu_c03 =param0(3,2);
    
    sig_eps_a01=param0(1,3);
    sig_eps_a02=param0(2,3);
    sig_eps_a03=param0(3,3);
    
weight_j=[0.322; 0.188; 0.05];


% Adjustement of initial search area (beyond 5 percent)

power_p0 = 1.0;
power_mu_c = 1.;
power_sig_eps_a = 1.;
power_rho_a = 1;

% save SMM

% create vector for initial parameter values
vec0 = [p01; p02; p03; mu_c01; mu_c02; mu_c03;sig_eps_a01; sig_eps_a02; sig_eps_a03];

% Keep track of results
conv_SMM = zeros(0,3);
xopt_SMM = 100000; % start iteration
conv_opt = zeros(3,6);
SMM_count = 0; % counter

% tolerance level
tol_SMM = 5e-3;
scale=[actual_var];


save('SMM','vec0','p01','p02','p03','mu_c01','mu_c02','mu_c03','sig_eps_a01','sig_eps_a02','sig_eps_a03',...
        'power_p0','power_mu_c','power_sig_eps_a','power_rho_a',...
    'conv_SMM','xopt_SMM','conv_opt','SMM_count',...
    'tol_SMM','-append', 'weight_j')
% 'power_phi','phi_init','phi0'

%% Options
%  Display, TolX, TolFun, MaxFunEvals

%% Set calibration target
% std.Y, autocorr(Y), frequency of default:
moment1 = actual_moments(1,1);
moment2 = actual_moments(1,2);
moment3 = actual_moments(1,3);
moment4 = actual_moments(1,4); 
moment5 = actual_moments(1,5);

moment6 = actual_moments(2,1);
moment7 = actual_moments(2,2);
moment8 = actual_moments(2,3);
moment9 = actual_moments(2,4); 
moment10 = actual_moments(2,5);

moment11 = actual_moments(3,1);
moment12 = actual_moments(3,2);
moment13 = actual_moments(3,3);
moment14 = actual_moments(3,4); 
moment15 = actual_moments(3,5);

target = [moment1; moment2; moment3; moment4; moment5; moment6; moment7; moment8; moment9; moment10; moment11; moment12; moment13; moment14; moment15];
save('SMM','target', 'scale','-append')
%% Start minimization routine 

%options = optimset('MaxIter',30);%, 'TolX', 1e-4,'Tolfun', 1e-3);
options = optimset('TolX', 1e-4,'Tolfun', 1e-3);
options = optimset('MaxIter',20);
options = optimset('MaxFunEvals',100);

[x,fval, exiflag] = fminsearch(@SMMmain,vec0, options);
res_estim_prod3l(jj,:)=[x' fval exiflag target' ]

 
  save res_estim_prod3l res_estim_prod3l ;

end